#ifndef __LUOO_SESSION_POOL_IMPL_HPP__
#define __LUOO_SESSION_POOL_IMPL_HPP__

#include <queue>
#include <hash_map>

#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>

namespace luoo
{
	template <typename session_type>
	class session_pool_impl : public boost::noncopyable
	{
	public:
		enum { INVALID_SESSION_ID  = -1 };
		typedef boost::shared_ptr<session_type> session_sptr;

	private:
		typedef stdext::hash_map<unsigned int, session_sptr> all_sessions;
		typedef std::queue<unsigned int> session_id_pool;

	public:
		explicit session_pool_impl(std::size_t pool_size)
			: pool_size_(pool_size)
		{
			for (std::size_t i = 0; i < pool_size_; ++i)
			{
				session_id_pool_.push(i);
			}
		}

		unsigned int push(session_sptr new_session)
		{
//			boost::mutex::scoped_lock lock(mutex_);
			if (session_id_pool_.empty())
			{
				return INVALID_SESSION_ID;
			}
			unsigned int id = session_id_pool_.front();
			if (all_sessions_.end() != all_sessions_.find(id))
			{
				return INVALID_SESSION_ID;
			}

			session_id_pool_.pop();
			all_sessions_.insert(std::make_pair(id, new_session));

			return id;
		}

		void pop(unsigned int session_id)
		{
//			boost::mutex::scoped_lock lock(mutex_);
			all_sessions_.erase(session_id);
			session_id_pool_.push(session_id);
		}

	private:
//		boost::mutex mutex_;
		std::size_t pool_size_;
		all_sessions all_sessions_;
		session_id_pool session_id_pool_;
	};
}//namespace luoo

#endif//__LUOO_SESSION_POOL_IMPL_HPP__